package com.leetcode;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 71. 简化路径
 * 双端队列
 *
 * @author fy
 * @date 2022/4/12 18:55
 */
public class Solution71_1 {

    public String simplifyPath(String path) {
        String[] paths = path.split("/");
        Deque<String> stack = new ArrayDeque<>();
        for (String s : paths) {
            if ("..".equals(s)) {
                if (!stack.isEmpty()) {
                    stack.pollLast();
                }
            } else if (!".".equals(s) && s.length() > 0) {
                stack.offerLast(s);
            }
        }
        StringBuilder sb = new StringBuilder();
        if (stack.isEmpty()) {
            sb.append("/");
        } else {
            while (!stack.isEmpty()) {
                sb.append("/").append(stack.pollFirst());
            }
        }
        return sb.toString();
    }

}
